{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Triangular Sum of an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #combinatorics #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #组合数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: triangularSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组的三角和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;，其中&nbsp;<code>nums[i]</code>&nbsp;是 <code>0</code>&nbsp;到 <code>9</code>&nbsp;之间（两者都包含）的一个数字。</p>\n",
    "\n",
    "<p><code>nums</code>&nbsp;的 <strong>三角和</strong>&nbsp;是执行以下操作以后最后剩下元素的值：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>nums</code>&nbsp;初始包含&nbsp;<code>n</code>&nbsp;个元素。如果&nbsp;<code>n == 1</code>&nbsp;，<strong>终止</strong>&nbsp;操作。否则，<strong>创建</strong>&nbsp;一个新的下标从&nbsp;<strong>0</strong>&nbsp;开始的长度为 <code>n - 1</code>&nbsp;的整数数组&nbsp;<code>newNums</code>&nbsp;。</li>\n",
    "\t<li>对于满足&nbsp;<code>0 &lt;= i &lt;&nbsp;n - 1</code>&nbsp;的下标&nbsp;<code>i</code>&nbsp;，<code>newNums[i]</code> <strong>赋值</strong>&nbsp;为&nbsp;<code>(nums[i] + nums[i+1]) % 10</code>&nbsp;，<code>%</code>&nbsp;表示取余运算。</li>\n",
    "\t<li>将&nbsp;<code>newNums</code>&nbsp;<strong>替换</strong> 数组&nbsp;<code>nums</code>&nbsp;。</li>\n",
    "\t<li>从步骤 1 开始&nbsp;<strong>重复</strong>&nbsp;整个过程。</li>\n",
    "</ol>\n",
    "\n",
    "<p>请你返回&nbsp;<code>nums</code>&nbsp;的三角和。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png\" style=\"width: 250px; height: 250px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [1,2,3,4,5]\n",
    "<b>输出：</b>8\n",
    "<strong>解释：</strong>\n",
    "上图展示了得到数组三角和的过程。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [5]\n",
    "<b>输出：</b>5\n",
    "<b>解释：</b>\n",
    "由于 nums 中只有一个元素，数组的三角和为这个元素自己。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 9</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-triangular-sum-of-an-array](https://leetcode.cn/problems/find-triangular-sum-of-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-triangular-sum-of-an-array](https://leetcode.cn/problems/find-triangular-sum-of-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,5]', '[5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        result, n, c = 0, len(nums), 1\n",
    "        for i in range(n):\n",
    "            result = (result + nums[i] * c) % 10\n",
    "            c = c *(n-i-1) // (i+1)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        ans, n, c = 0, len(nums) - 1, 1\n",
    "        for i, num in enumerate(nums):\n",
    "            ans = (ans + c * num) % 10\n",
    "            c = (c * n) // (i+1)\n",
    "            n -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        l = len(nums) - 1\n",
    "        return sum(comb(l, i) * n for i, n in enumerate(nums)) % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while n > 0:\n",
    "            n -= 1\n",
    "            for i in range(n):\n",
    "                nums[i] = (nums[i] + nums[i + 1]) % 10\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        ans, n = 0, len(nums) - 1\n",
    "        for i, num in enumerate(nums):\n",
    "            ans = (ans + comb(n, i) * num) % 10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        # if len(nums) == 1:\n",
    "        #     return nums[0]\n",
    "        n = len(nums)\n",
    "        while n>1:\n",
    "            for i in range(len(nums)-1,len(nums)-n,-1):\n",
    "                nums[i] = (nums[i]+nums[i-1]) % 10\n",
    "            n-=1\n",
    "            # print(nums)\n",
    "        return nums[-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\r\n",
    "class Solution:\r\n",
    "    def triangularSum(self, nums: List[int]) -> int:\r\n",
    "        # n = len(nums)\r\n",
    "        # result = 0\r\n",
    "        # if n <= 1:\r\n",
    "        #     return nums[0]\r\n",
    "    \r\n",
    "        # m = n // 2\r\n",
    "        # for i in range(m):\r\n",
    "        #     result = result + (nums[i] + nums[n-1-i]) % 10\r\n",
    "        #     result = result % 10\r\n",
    "        # return result\r\n",
    "        \r\n",
    "        n = len(nums)\r\n",
    "        result = 0\r\n",
    "        ratio = 1\r\n",
    "        for index in range(n//2 + n%2):\r\n",
    "            \r\n",
    "            # X_n+1 = (N-K)/(k+1)*X_n\r\n",
    "            # ratio = math.comb(n-1, index)\r\n",
    "            if index != 0:\r\n",
    "                ratio = ratio*(n-index)//(index)\r\n",
    "                # print(index,ratio)\r\n",
    "            if 2*index+1 == n:\r\n",
    "                result += (nums[index])* ratio\r\n",
    "            else:\r\n",
    "                result += (nums[index] + nums[n-1-index])* ratio \r\n",
    "        \r\n",
    "        return result % 10\r\n",
    "    \r\n",
    "    \r\n",
    "        # length = len(nums)\r\n",
    "        # rns = 0\r\n",
    "        # #temp 就是C (N index)的值\r\n",
    "        # temp = 1 \r\n",
    "        # for index in range(length):\r\n",
    "        #     if index != 0:\r\n",
    "        #         temp = temp * (length  - index) // index\r\n",
    "        #     rns += nums[index] * temp\r\n",
    "        # return rns % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        return sum(comb(len(nums) - 1, i) * n for i, n in enumerate(nums)) % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if n == 1:\n",
    "            return nums[0] % 10\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += comb(n - 1, i) * nums[i]\n",
    "        \n",
    "        res %= 10\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        n = len(nums)\n",
    "        while n>1:\n",
    "            for i in range(len(nums)-1,len(nums)-n,-1):\n",
    "                nums[i] = (nums[i]+nums[i-1]) % 10\n",
    "            n-=1\n",
    "            # print(nums)\n",
    "        return nums[-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        lenn = len(nums)\n",
    "        for i in range(lenn):\n",
    "            for j in range(lenn - i - 1):\n",
    "                nums[j] = (nums[j] + nums[j + 1]) % 10 \n",
    "        return nums[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        #copy\n",
    "        for i in range(len(nums)-1,0,-1):\n",
    "            for j in range(i):\n",
    "                nums[j] = (nums[j] + nums[j+1])%10\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        l = len(nums)\n",
    "        if l!=1:\n",
    "            for i in range(l):\n",
    "                x = 1\n",
    "                c = min(i,l-i-1)\n",
    "                for j in range(c):\n",
    "                    x *= (l-1-j)/(j+1)\n",
    "                    x = x%10\n",
    "                x = comb(l-1,c)\n",
    "                res += (x * nums[i])\n",
    "            return int(res%10)\n",
    "        else:\n",
    "            return nums[0]%10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while n > 0:\n",
    "            n -= 1\n",
    "            for i in range(n):\n",
    "                nums[i] = (nums[i] + nums[i + 1]) % 10\n",
    "        return nums[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        l = len(nums)\n",
    "        if l!=1:\n",
    "            for i in range(l):\n",
    "                x = 1\n",
    "                c = min(i,l-i-1)\n",
    "                for j in range(c):\n",
    "                    x *= (l-1-j)\n",
    "                for j in range(c):\n",
    "                    x //= (j+1)\n",
    "                x = round(x)\n",
    "                # x = comb(l-1,c)\n",
    "                res += (x * nums[i])\n",
    "            return int(res%10)\n",
    "        else:\n",
    "            return nums[0]%10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums)!=1:\n",
    "            n = len(nums) # 原始长度\n",
    "            for i in range(len(nums)-1):\n",
    "                nums.append((nums[i]+nums[i+1])%10)\n",
    "            nums = nums[n:]\n",
    "        \n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        for n in range(len(nums) - 1, 0, -1):\n",
    "            for i in range(n):\n",
    "                nums[i] = (nums[i] + nums[i + 1]) % 10\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return nums[0]\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            k = min(i, n - i -1)\n",
    "            ans = (ans + nums[i] * math.comb(n - 1, k)) % 10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "\n",
    "        \n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(len(nums) - i - 1):\n",
    "                nums[j] = (nums[j] + nums[j + 1]) % 10\n",
    "\n",
    "        \n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        ret = 0\n",
    "        c_m_n = 1\n",
    "        n = len(nums) - 1\n",
    "        for m in range(n + 1):\n",
    "            #print(m)\n",
    "            #print(c_m_n)\n",
    "        \n",
    "            c_m_n = math.factorial(n)//(math.factorial(m)*math.factorial(n-m))\n",
    "            ret += c_m_n * nums[m]\n",
    "            #print(c_m_n * nums[m], c_m_n, nums[m])\n",
    "            ret %= 10\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\r\n",
    "class Solution:\r\n",
    "    def triangularSum(self, nums: List[int]) -> int:\r\n",
    "        # n = len(nums)\r\n",
    "        # result = 0\r\n",
    "        # if n <= 1:\r\n",
    "        #     return nums[0]\r\n",
    "    \r\n",
    "        # m = n // 2\r\n",
    "        # for i in range(m):\r\n",
    "        #     result = result + (nums[i] + nums[n-1-i]) % 10\r\n",
    "        #     result = result % 10\r\n",
    "        # return result\r\n",
    "        \r\n",
    "        n = len(nums)\r\n",
    "        result = 0\r\n",
    "        for index in range(n//2 + n%2):\r\n",
    "            ratio = math.comb(n-1, index)\r\n",
    "            if 2*index+1 == n:\r\n",
    "                result += (nums[index])* ratio \r\n",
    "            else:\r\n",
    "                result += (nums[index] + nums[n-1-index])* ratio \r\n",
    "        \r\n",
    "        return result % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(len(nums) - i - 1):\n",
    "                nums[j] = (nums[j] + nums[j + 1]) % 10\n",
    "        \n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        while n>1:\n",
    "            for i in range(n-1):\n",
    "                nums[i]=(nums[i+1]+nums[i])%10\n",
    "            n-=1\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        c_n_x = 1\n",
    "        rtn = 0\n",
    "        for i in range(n):\n",
    "            if i:\n",
    "                c_n_x *= (n - i)\n",
    "                c_n_x //= i\n",
    "            rtn += (c_n_x * nums[i]) % 10\n",
    "        return rtn % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        rd = nums.__len__()-1\n",
    "        while rd>0:\n",
    "            n = rd+1\n",
    "            for i in range(1, n):\n",
    "                nums[i-1] = (nums[i-1]+nums[i]) % 10\n",
    "            rd -=1\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)-1):\n",
    "            for j in range(len(nums)-1-i):\n",
    "                nums[j] = (nums[j+1] + nums[j])%10\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if n == 1:\n",
    "            return nums[0] % 10\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += comb(n - 1, i) * nums[i]\n",
    "        \n",
    "        res %= 10\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            new_nums = list()\n",
    "            for i in range(len(nums) - 1):\n",
    "                new_nums.append((nums[i] + nums[i + 1]) % 10)\n",
    "            nums = new_nums\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        while len(nums) != 1:\n",
    "            temp = []\n",
    "            for i in range(len(nums)-1):\n",
    "                temp.append((nums[i] + nums[i+1])%10)\n",
    "            nums = temp\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans, k = 0, 1\n",
    "        for i in range(n):\n",
    "            ans += nums[i] * k\n",
    "            k = k * (n - 1 - i) // (i + 1)\n",
    "        return ans % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while n > 1:\n",
    "            for i in range(0, n-1):\n",
    "                nums[i] += nums[i+1]\n",
    "                nums[i] %= 10\n",
    "            n -= 1\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums)==1:\n",
    "            return nums[0]\n",
    "        while True:\n",
    "            temp2 = []\n",
    "            for a,b in pairwise(nums):\n",
    "                temp2.append((a+b)%10)\n",
    "            if len(temp2) == 1:\n",
    "                return temp2[0]\n",
    "            nums = temp2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "\n",
    "        # while len(nums) > 1:\n",
    "        #     nums = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "        # return nums[0]\n",
    "\n",
    "        n = len(nums) - 1\n",
    "        for k in range(n):\n",
    "            for i in range(n-k):\n",
    "                nums[i] = (nums[i] + nums[i+1]) % 10\n",
    "        return nums[0]\n",
    "\n",
    "   \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            i, n = 0, len(nums)\n",
    "            while i < n - 1:\n",
    "                nums[i] = (nums[i] + nums[i+1]) % 10\n",
    "                i += 1\n",
    "            nums.pop()\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        for i in range(1,n):\n",
    "            NUM=[]\n",
    "            for j in range(n-i):\n",
    "                NUM+=[(nums[j]+nums[j+1])%10]\n",
    "            nums=NUM\n",
    "        return nums[0]\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while len(nums) > 1:\n",
    "            new = list()\n",
    "            for i in range(len(nums)-1):\n",
    "                new.append((nums[i] + nums[i+1]) %10)\n",
    "            nums = new \n",
    "        return nums [0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def cn(self, n, m):\n",
    "        if(m == 0 or m == n):\n",
    "            return 1\n",
    "        s = 1\n",
    "        for i in range(m + 1, n + 1):\n",
    "            s *= i\n",
    "        t = 1\n",
    "        for i in range(1, n - m + 1):\n",
    "            t *= i\n",
    "        return s // t\n",
    "\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        # 杨辉三角\n",
    "        l = len(nums)\n",
    "        if(l == 1):\n",
    "             return nums[0]\n",
    "        ans = 0\n",
    "        for i in range(l):\n",
    "            ans += (self.cn(l - 1, i) * nums[i]) % 10\n",
    "        return int(ans) % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums)==1:\n",
    "            return nums[0]\n",
    "        while True:\n",
    "            temp2 = []\n",
    "            for a,b in pairwise(nums):\n",
    "\n",
    "                temp2.append((a+b)%10)\n",
    "            if len(temp2) == 1:\n",
    "                return temp2[0]\n",
    "            nums = temp2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        while n>3:\n",
    "            s1=[]\n",
    "            for j in range(len(nums)-2):\n",
    "                s1.append(nums[j]+nums[j+1]*2+nums[j+2])\n",
    "            nums=s1\n",
    "            n=len(nums)\n",
    "        if n==3:\n",
    "            num=nums[0]+nums[1]*2+nums[2]\n",
    "        if n==2:\n",
    "            num=nums[0]+nums[1]\n",
    "        if n==1:\n",
    "            num=nums[0]\n",
    "        return num%10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums)==1:\n",
    "            return nums[0]\n",
    "        while True:\n",
    "            temp2 = []\n",
    "            for a,b in pairwise(nums):\n",
    "                temp2.append((a+b)%10)\n",
    "            if len(temp2) == 1:\n",
    "                return temp2[0]\n",
    "            nums = temp2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\r\n",
    "class Solution:\r\n",
    "    def triangularSum(self, nums: List[int]) -> int:\r\n",
    "        # n = len(nums)\r\n",
    "        # result = 0\r\n",
    "        # if n <= 1:\r\n",
    "        #     return nums[0]\r\n",
    "    \r\n",
    "        # m = n // 2\r\n",
    "        # for i in range(m):\r\n",
    "        #     result = result + (nums[i] + nums[n-1-i]) % 10\r\n",
    "        #     result = result % 10\r\n",
    "        # return result\r\n",
    "        \r\n",
    "        n = len(nums)\r\n",
    "        result = 0\r\n",
    "        ratio = 1\r\n",
    "        for index in range(n//2 + n%2):\r\n",
    "            \r\n",
    "            # X_n+1 = (N-K)/(k+1)*X_n\r\n",
    "            # ratio = math.comb(n-1, index)\r\n",
    "            if index != 0:\r\n",
    "                ratio = ratio*(n-index)//(index)\r\n",
    "                # print(index,ratio)\r\n",
    "            if 2*index+1 == n:\r\n",
    "                result += (nums[index])* ratio\r\n",
    "            else:\r\n",
    "                result += (nums[index] + nums[n-1-index])* ratio \r\n",
    "        \r\n",
    "        return result % 10\r\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        def f(nums):\n",
    "            n = len(nums)\n",
    "            newNum = []\n",
    "            for i in range(n - 1):\n",
    "                newNum.append((nums[i] + nums[i + 1]) % 10)\n",
    "            return newNum\n",
    "\n",
    "        while len(nums) > 1:\n",
    "            nums = f(nums)\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)-1, 0, -1):\n",
    "            for j in range(i):\n",
    "                nums[j] = (nums[j]+nums[j+1])%10\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            new_nums = list()\n",
    "            for i in range(len(nums) - 1):\n",
    "                new_nums.append((nums[i] + nums[i + 1]) % 10)\n",
    "            nums = new_nums\n",
    "        return nums[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        # if len(nums) == 1:\n",
    "        #     return nums[0]\n",
    "        \n",
    "        # new_num = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "        # return self.triangularSum(new_num)\n",
    "\n",
    "        while len(nums) > 1:\n",
    "            new_num = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "            nums = new_num\n",
    "        return nums[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        \n",
    "        if n == 1:\n",
    "            return nums[0] % 10\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += comb(n - 1, i) * nums[i]\n",
    "        \n",
    "        res %= 10\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        tmp = nums\n",
    "        while n > 1:\n",
    "            for i in range(n-1):\n",
    "                tmp[i] = (tmp[i] + tmp[i + 1]) % 10\n",
    "            n -= 1\n",
    "\n",
    "        return tmp[0]\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while n != 1:\n",
    "            tmp = []\n",
    "            for i in range(1, n):\n",
    "                tmp.append((nums[i-1]+nums[i])%10)\n",
    "            nums = tmp[:]\n",
    "            n -= 1\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        # if len(nums)==1:\n",
    "        #     return nums[0]\n",
    "        while len(nums)!=1:\n",
    "            tmp=[]\n",
    "            for i in range(len(nums)-1):\n",
    "                cur=nums[i]+nums[i+1]\n",
    "                cur%=10\n",
    "                tmp.append(cur)\n",
    "            nums=tmp\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        for i in range(1, n):\n",
    "            for j in range(n-i):\n",
    "                nums[j] = (nums[j] + nums[j+1]) % 10\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        ne = []\n",
    "        while len(nums) > 1:\n",
    "            for i in range(len(nums) - 1):\n",
    "                ne.append((nums[i] + nums[i+1]) % 10)\n",
    "            nums, ne = ne, []\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while True:\n",
    "            if len(nums) == 1:\n",
    "                break\n",
    "            tmp = []\n",
    "            for index, value in enumerate(nums[:-1]):\n",
    "                tmp.append((value + nums[index + 1]) % 10)\n",
    "\n",
    "            nums[:] = tmp\n",
    "\n",
    "        return nums[0]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1 :\n",
    "            new_nums = []\n",
    "            for i in range(len(nums)-1):\n",
    "                new_nums.append((nums[i]+nums[i+1])%10)\n",
    "            nums = new_nums\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        N = len(nums) - 1\n",
    "        return sum([(comb(N, i) * num) % 10 for i, num in enumerate(nums)]) % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) != 1:\n",
    "            res = [0]*(len(nums)-1)\n",
    "            for i in range(len(nums)-1):\n",
    "                res[i] = nums[i]+nums[i+1]\n",
    "            nums = res\n",
    "        return nums[0]%10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            new = []\n",
    "            for i in range(len(nums)-1):\n",
    "                v = (nums[i] + nums[i+1]) % 10\n",
    "                new.append(v)\n",
    "            nums = new\n",
    "        \n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        _list = nums\n",
    "        for i in range(n-1):\n",
    "            _tmp = []\n",
    "            for j in range(1,len(_list)):\n",
    "                _tmp.append((_list[j]+_list[j-1]) % 10)\n",
    "            _list = _tmp\n",
    "            # print(_list)\n",
    "        return _list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        while len(nums) > 1:\n",
    "            tmp = nums\n",
    "            nums = list()\n",
    "            n = len(tmp)\n",
    "            for i in range(n - 1):\n",
    "                nums.append((tmp[i] + tmp[i + 1]) % 10)\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        while n > 1:\n",
    "            item = []\n",
    "            for i in range(1,n):\n",
    "                item.append((nums[i]+nums[i-1])%10)\n",
    "            nums = item\n",
    "            n = len(nums)\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        newNums=[]\n",
    "        while len(nums)!=1:\n",
    "            newNums.clear()\n",
    "            for i in range(len(nums)-1):\n",
    "                newNums.append((nums[i] + nums[i+1]) % 10)\n",
    "            nums=newNums[:]\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        # if len(nums) == 1:\n",
    "        #     return nums[0]\n",
    "        \n",
    "        # new_num = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "        # return self.triangularSum(new_num)\n",
    "\n",
    "        while len(nums) > 1:\n",
    "            new_num = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "            nums = new_num.copy()\n",
    "        return nums[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            new_nums = list()\n",
    "            for i in range(len(nums) - 1):\n",
    "                new_nums.append((nums[i] + nums[i + 1]) % 10)\n",
    "            nums = new_nums\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "\n",
    "        while len(nums)>1:\n",
    "            new_nums = []\n",
    "            for i in range(len(nums)-1):\n",
    "                new_nums.append((nums[i]+nums[i+1])%10)\n",
    "            nums = new_nums\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        while len(nums) > 1:\n",
    "            newNums = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "            nums = newNums\n",
    "        return nums[0]\n",
    "\n",
    "# 示例\n",
    "sol = Solution()\n",
    "print(sol.triangularSum([2, 5, 3, 7]))  # 这应该会返回一个0到9之间的整数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n==1:\n",
    "            return nums[0]\n",
    "        begin = nums[:]\n",
    "        while n>1:\n",
    "            temp =[]\n",
    "            for i in range(len(begin)-1):\n",
    "                temp.append((begin[i]+begin[i+1])%10)\n",
    "            begin = temp[:]\n",
    "            temp = []\n",
    "            n = n-1\n",
    "        return begin[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\r\n",
    "class Solution:\r\n",
    "    def triangularSum(self, nums: List[int]) -> int:\r\n",
    "        # n = len(nums)\r\n",
    "        # result = 0\r\n",
    "        # if n <= 1:\r\n",
    "        #     return nums[0]\r\n",
    "    \r\n",
    "        # m = n // 2\r\n",
    "        # for i in range(m):\r\n",
    "        #     result = result + (nums[i] + nums[n-1-i]) % 10\r\n",
    "        #     result = result % 10\r\n",
    "        # return result\r\n",
    "        \r\n",
    "        n = len(nums)\r\n",
    "        result = 0\r\n",
    "        ratio = 1\r\n",
    "        for index in range(n//2 + n%2):\r\n",
    "            \r\n",
    "            # X_n+1 = (N-K)/(k+1)*X_n\r\n",
    "            # ratio = math.comb(n-1, index)\r\n",
    "            if index != 0:\r\n",
    "                ratio = ratio*(n-index)//(index)\r\n",
    "                print(index,ratio)\r\n",
    "            if 2*index+1 == n:\r\n",
    "                result += (nums[index])* ratio\r\n",
    "            else:\r\n",
    "                result += (nums[index] + nums[n-1-index])* ratio \r\n",
    "        \r\n",
    "        return result % 10\r\n",
    "    \r\n",
    "    \r\n",
    "        # length = len(nums)\r\n",
    "        # rns = 0\r\n",
    "        # #temp 就是C (N index)的值\r\n",
    "        # temp = 1 \r\n",
    "        # for index in range(length):\r\n",
    "        #     if index != 0:\r\n",
    "        #         temp = temp * (length  - index) // index\r\n",
    "        #     rns += nums[index] * temp\r\n",
    "        # return rns % 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        while len(nums)>1:\n",
    "            res=[]\n",
    "            for i in range(0,len(nums)-1):\n",
    "                res.append((nums[i]+nums[i+1])%10)\n",
    "            nums=res.copy()\n",
    "        return nums[0]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        res=[nums]\n",
    "        for i in range(1,n):\n",
    "            NUM=[]\n",
    "            for j in range(n-i):\n",
    "                NUM+=[(res[i-1][j]+res[i-1][j+1])%10]\n",
    "            res+=[NUM]\n",
    "        return res[n-1][0]\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        res=[nums]\n",
    "        for i in range(1,n):\n",
    "            NUM=[]\n",
    "            for j in range(n-i):\n",
    "                NUM+=[(res[i-1][j]+res[i-1][j+1])%10]\n",
    "            res+=[NUM]\n",
    "        return res[n-1][0]\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        else :\n",
    "            result = []\n",
    "            for i in range(len(nums) - 1):\n",
    "                sum_of_two = nums[i] + nums[i + 1]\n",
    "                result.append(sum_of_two%10)\n",
    "            return self.triangularSum(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "        \n",
    "        new_num = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums)-1)]\n",
    "        return self.triangularSum(new_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        if n==1:\n",
    "            return nums[0]\n",
    "        else:\n",
    "            newnums=[(nums[i]+nums[i+1])%10 for i in range(n-1)]\n",
    "            return self.triangularSum(newnums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def triangularSum(self, nums: List[int]) -> int:\n",
    "        if len(nums)==1:\n",
    "            return nums[0]\n",
    "        else:\n",
    "            temp=[]\n",
    "            for i in range(len(nums)-1):\n",
    "                temp.append((nums[i]+nums[i+1])%10)\n",
    "            return self.triangularSum(temp)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
